%% Codes for the article "Dissection Mechanisms of Financial Crises: Intermediation and Sentiment", by Arvind Krishnamurthy and Wenhao Li.
%% Set Path and Add Path
% clear all;
mainpath = fileparts(mfilename('fullpath'));
addpath(genpath([ mainpath, '/Codes_Model' ] ))
cd(mainpath);

%% ------------------- Generate Main Simulation Results. This will take about half an hour to run. -----------------------------------------------
N_years = 25000;   % The # of years is the same as the calibration moment calculations. 
par_init =  initialization();
warning('off');

S1 = load( 'solutions/baseline_benchmark.mat' );
S2 = load( 'solutions/baseline_rational.mat' ) ; 
S3 = load( 'solutions/baseline_behavioral.mat' );

% *** Benchmark Model
model_sol = post_processing(S1.model_sol, true);
save('solutions/post_processed/baseline_benchmark_post_processed.mat', 'model_sol');
disp( '***************** Simulation of the Benchmark Model ***************'  )
for( seed = 1:4 )   % iternation number
    rng(seed)
    delete( ['simulated data/smolayak_benchmark_sim', num2str(seed) ,'.xlsx'] );
    moments1 = simulation_results_output( S1, N_years, ['simulated data/smolayak_benchmark_sim', num2str(seed) ,'.xlsx'] );
end

% *** Rational Model **** 
model_sol = post_processing(S2.model_sol, true);
model_sol.kappa_lambda_vec = model_sol.par.kappa_lambda_fun(model_sol.par.lambda_grid);
save('solutions/post_processed/baseline_Bayesian_post_processed.mat', 'model_sol');
disp( '***************** Simulation of the Bayesian Model ***************'  )
for( seed = 1:4 )   % iternation number
    rng(seed)
    delete( ['simulated data/smolayak_Bayesian_sim', num2str(seed) ,'.xlsx'] );
    moments2 = simulation_results_output( S2, N_years, ['simulated data/smolayak_Bayesian_sim', num2str(seed) ,'.xlsx'] );
end

% *** Behavioral Model
model_sol = post_processing(S3.model_sol, true);
save('solutions/post_processed/baseline_Diagnostic_post_processed.mat', 'model_sol');
disp( '***************** Simulation of the Diagnostic Model ***************'  )
for( seed = 1:4 )   % iternation number
    rng(seed)
    delete( ['simulated data/smolayak_Diagnostic_sim', num2str(seed) ,'.xlsx'] );
    moments3 = simulation_results_output( S3, N_years, ['simulated data/smolayak_Diagnostic_sim', num2str(seed) ,'.xlsx'] );
end


%% For the Appendix
% Generat simulated data for the general belief cases. 
S4 = load('solutions/baseline_optimistic.mat');
S5 = load('solutions/baseline_pessimistic.mat');

% *** Optimistic model
model_sol = post_processing(S4.model_sol, true);
save('solutions/post_processed/baseline_optimistic_post_processed.mat', 'model_sol');
disp( '***************** simulation of the optimistic model ***************'  )
for( seed = 1:4 )   % iternation number
    rng(seed)
    delete( ['simulated data/smolayak_optimistic_sim', num2str(seed) ,'.xlsx'] );
    simulation_results_output( S4, N_years, ['simulated data/smolayak_optimistic_sim', num2str(seed) ,'.xlsx'] );
end

% *** Pessimistic model 
model_sol = post_processing(S5.model_sol, true);
save('solutions/post_processed/baseline_pessimistic_post_processed.mat', 'model_sol');
disp( '***************** simulation of the pessimistic model ***************'  )
for( seed = 1:4 )   % iternation number
    rng(seed)
    delete( ['simulated data/smolayak_pessimistic_sim', num2str(seed) ,'.xlsx'] );
    simulation_results_output( S5, N_years, ['simulated data/smolayak_pessimistic_sim', num2str(seed) ,'.xlsx'] );
end
close all;

